package com.shen.my_blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shen.my_blog.dao.mapper.CommentsMapper;
import com.shen.my_blog.dao.pojo.Comment;
import com.shen.my_blog.dao.pojo.SysUser;
import com.shen.my_blog.service.CommentsService;
import com.shen.my_blog.service.SysUserService;
import com.shen.my_blog.utils.UserThreadLocal;
import com.shen.my_blog.vo.CommentVo;
import com.shen.my_blog.vo.Result;
import com.shen.my_blog.vo.UserVo;
import com.shen.my_blog.vo.params.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    CommentsMapper commentsMapper;
    @Autowired
    SysUserService sysUserService;

    @Override
    public Result commentsByArticleId(Long articleId) {
        /**
         * 1、查询文章id   查询 评论列表 从comment 表中查询
         * 2、根据作者的id  查询作者的信息
         * 3、判断 如果level = 1 要去查询他有没有子评论
         * 4、如果有 根据评论id  进行查询（parent_id）
         */
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getArticleId,articleId);
        wrapper.eq(Comment::getLevel,1);
        List<Comment> comments = commentsMapper.selectList(wrapper);
        List<CommentVo> commentVoList = copyList(comments);
        return Result.success(commentVoList);
    }

    /**
     * 评论功能
     * @param commentParam
     * @return
     */
    @Override
    public Result comment(CommentParam commentParam) {
        //获取当前用户
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        //获取当前时间
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0){
            comment.setLevel(1);
        }else {
            comment.setLevel(2);
        }
        //如果是为空 parent=0
        comment.setParentId(parent == null ? 0 :parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        commentsMapper.insert(comment);
        return Result.success(null);
    }


    public List<CommentVo> copyList(List<Comment> commentList){
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        //相同属性copy
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        commentVo.setId(Long.valueOf(String.valueOf(comment.getId())));
        //作者信息
        Long authorId = comment.getAuthorId();
        UserVo userVo  = sysUserService.findUserVoById(authorId);
        commentVo.setAuthor(userVo);
        //子评论
        Integer level = comment.getLevel();
        if (1 == level){
            Long id = comment.getId();
            List<CommentVo> commentVoList = findCommentsByParentId(id);
            commentVo.setChildrens(commentVoList);
        }
        //to User 给谁评论
        if (level >1){
            Long toUid = comment.getToUid();
            UserVo toUserVo = sysUserService.findUserVoById(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long id) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getParentId,id);
        wrapper.eq(Comment::getLevel,2);
        List<Comment> comments = commentsMapper.selectList(wrapper);
        return copyList(comments);
    }

}
